package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    /* @Reference
     MenuService menuService;*/
    @Reference
    RoleService roleService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/findAll")
    private Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }
    @RequestMapping("/findById")
    private Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }

    }
    @RequestMapping("/delete")
    private Result delete(Integer id) {
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }

    }

    @RequestMapping("/add")
    private Result add(Integer[] ids, @RequestBody Map<String, Object> map) {

        try {
            List<Integer> users = (List<Integer>) map.get("users");
            Map map1 = (Map) map.get("formData");
            String name = (String) map1.get("name");
            String keyword = (String) map1.get("keyword");
            String description = (String) map1.get("description");
            Role role = new Role();
            role.setName(name);
            role.setKeyword(keyword);
            role.setDescription(description);
            roleService.add(ids, users, role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }

    }
    @RequestMapping("/edit")
    private Result edit(Integer[] ids, @RequestBody Map<String, Object> map) {
        try {
            List<Integer> users = (List<Integer>) map.get("users");
            Map map1 = (Map) map.get("formData");
            String name = (String) map1.get("name");
            String keyword = (String) map1.get("keyword");
            Integer id = (Integer) map1.get("id");
            String description = (String) map1.get("description");
            Role role = new Role();
            role.setName(name);
            role.setId(id);
            role.setKeyword(keyword);
            role.setDescription(description);

            roleService.edit(ids, users, role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }

    }
}
